# Copyright (c) 2025 Li Auto Inc. and its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# binary-gcc.mk
#

define transform-o-to-executable
@mkdir -p $(dir $@)
@echo "LD	$@"
$(hide)$(CC)							\
        $(filter-out -l%, $(PRIVATE_LFLAGS))			\
        $(if $(strip $(PRIVATE_LDS)), -T $(PRIVATE_LDS),)	\
        -o $@							\
        $(PRIVATE_ALL_OBJS)					\
        -Wl,--whole-archive					\
        $(PRIVATE_LIBRARY)					\
        -Wl,--no-whole-archive					\
        -Wl,--start-group					\
        $(PRIVATE_LIBRARY)					\
        $(filter -l%, $(PRIVATE_LFLAGS))			\
        -Wl,--end-group
$(transform-o-to-executable-post)
endef

define transform-o-to-shared-lib
@mkdir -p $(dir $@)
@echo "LD	$@"
$(hide)$(CC)							\
       -Wl,-soname,$(notdir $@)					\
       $(PRIVATE_ALL_OBJS)					\
       $(PRIVATE_LFLAGS) -fPIC -shared				\
       $(if $(strip $(PRIVATE_LIBRARY)),			\
            -Wl$(comma)--whole-archive				\
            $(PRIVATE_LIBRARY)					\
            -Wl$(comma)--no-whole-archive,)			\
       $(if $(strip $(PRIVATE_LIBRARY)),			\
            -Wl$(comma)--start-group				\
            $(PRIVATE_LIBRARY)					\
            -Wl$(comma)--end-group,)				\
       -o $@
endef

define transform-o-to-static-lib
@mkdir -p $(dir $@)
@echo "AR	$@"
$(hide)$(AR) -r $@ $(PRIVATE_ALL_OBJS)
endef

define transform-c-to-o
@mkdir -p $(dir $@)
@echo "CC	$<"
$(hide)$(CC) $(c-includes) $(PRIVATE_CFLAGS)	\
       -MD -MF $(patsubst %.o,%.d,$@) -fPIC -c $< -o $@
endef

define transform-cpp-to-o
@mkdir -p $(dir $@)
@echo "CXX	$<"
$(hide)$(CXX) $(c-includes) $(PRIVATE_CFLAGS)	\
       -MD -MF $(patsubst %.o,%.d,$@) -fPIC -c $< -o $@
endef

define transform-c-to-cmd
@echo "$@:$<" > $(patsubst %.o,%.cmd,$@)
@echo "$(CC) $(c-includes) $(PRIVATE_CFLAGS) -c $< -o $@" | tr '\n' ' ' | sed 's/\s\+/ /g' >> $(patsubst %.o,%.cmd,$@)
endef

define transform-cpp-to-cmd
@echo "$@:$<" > $(patsubst %.o,%.cmd,$@)
@echo "$(CXX) $(c-includes) $(PRIVATE_CFLAGS) -c $< -o $@" | tr '\n' ' ' | sed 's/\s\+/ /g' >> $(patsubst %.o,%.cmd,$@)
endef

# vim:ft=make
#
